package com.ming.sps.server.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.util.Collections;

public class AddressResolutionUtil {

    /**
     * 解析地址
     * @author lin
     * @param address
     * @return
     */
    public static List<Map<String,String>> addressResolution(String address){
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>[^街]+街|.+村|.+东|.+南|.+西|.+北|.+中)?(?<street>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null,village=null,street=null;
        List<Map<String,String>> table=new ArrayList<Map<String,String>>();
        Map<String,String> row=null;
        while(m.find()){
            row=new LinkedHashMap<String,String>();
            province=m.group("province");
            row.put("province", province==null?"":province.trim());
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
            county=m.group("county");
            row.put("county", county==null?"":county.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            street = m.group("street");
            row.put("street", street == null ? "" : street.trim());
            table.add(row);
        }
        return table;
    }

    public static void main(String[] args) {
        System.out.println(addressResolution("广东省广州市增城区增江街东桥东路115号"));
        System.out.println(addressResolution("广东省广州市天河区中山大道中6号"));
    }

}
